package org.linlinjava.litemall.admin.web;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.FormulaDTO;
import org.linlinjava.litemall.admin.dto.FormulaVO;
import org.linlinjava.litemall.admin.dto.MaterialVO;
import org.linlinjava.litemall.admin.service.AdminProductService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.Product;
import org.linlinjava.litemall.db.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Validated
public class AdminProductController {
    private final Log logger = LogFactory.getLog(AdminProductController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private AdminProductService adminProductService;

    @RequiresPermissions("admin:product:list")
    @RequiresPermissionsDesc(menu = {"物料管理", "产品管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(required = false) Long startTime,
                       @RequestParam(required = false) Long endTime,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<Product> brandList = productService.querySelective(id,startTime,endTime, name, page, limit, sort, order);
        if (CollectionUtils.isEmpty(brandList)) {
            return ResponseUtil.okList(brandList);
        }
        List<FormulaVO> formulaVOS = Lists.newArrayList();
        for (Product product : brandList) {
            FormulaVO formulaVO = new FormulaVO();
            formulaVO.setId(product.getId());
            formulaVO.setName(product.getName());
            formulaVO.setNumber(product.getNumber());
            formulaVO.setPrice(product.getPrice());
            formulaVO.setBrief(product.getBrief());
            formulaVO.setAddTime(product.getAddTime());
            String material1 = product.getMaterial();
            List<MaterialVO> materialAttributes = new Gson().fromJson(material1, new TypeToken<List<MaterialVO>>() {
            }.getType());
            formulaVO.setMaterialVOS(materialAttributes);
            formulaVOS.add(formulaVO);
        }
        return ResponseUtil.okList(formulaVOS);
    }


    @RequiresPermissions("admin:product:create")
    @RequiresPermissionsDesc(menu = {"物料管理", "产品管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody FormulaDTO brand) {
        if (brand == null) {
            return ResponseUtil.badArgument();
        }
        return adminProductService.addOne(brand);
    }

//    @RequiresPermissions("admin:product:read")
//    @RequiresPermissionsDesc(menu = {"物料管理", "产品管理"}, button = "详情")
//    @GetMapping("/read")
//    public Object read(@NotNull Integer id) {
//        Product brand = productService.findById(id);
//        return ResponseUtil.ok(brand);
//    }

//    @RequiresPermissions("admin:product:update")
//    @RequiresPermissionsDesc(menu = {"物料管理", "产品管理"}, button = "编辑")
//    @PostMapping("/update")
//    public Object update(@RequestBody Product brand) {
//        Object error = validate(brand);
//        if (error != null) {
//            return error;
//        }
//        if (productService.updateById(brand) == 0) {
//            return ResponseUtil.updatedDataFailed();
//        }
//        return ResponseUtil.ok(brand);
//    }

    @RequiresPermissions("admin:product:delete")
    @RequiresPermissionsDesc(menu = {"物料管理", "产品管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody Product brand) {
        Integer id = brand.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        productService.deleteById(id);
        return ResponseUtil.ok();
    }



}
